package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.MyDebtDetailActivity;
import com.kkd.kkdapp.http.entity.RegularDebtDetailDataItem;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.view.RoundProgressBar;

public class MyRegularDebtAdapter extends BaseAdapter {

	private final int POSITION_ONE = 0;

	private List<RegularDebtDetailDataItem> mList;
	private Context context;
	private int type;
	private PopupWindow mDialog;
	private Handler mHandler = new Handler();
	private int width_should;
	private DecimalFormat df = new DecimalFormat("###,##0.00");
	private DecimalFormat df_point2 = new DecimalFormat("0.00");

	public MyRegularDebtAdapter(Context context,
			List<RegularDebtDetailDataItem> mList, int type) {
		this.mList = mList;
		this.context = context;
		this.type = type;
		width_should = SystemUtils.dip2px(context, 38);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final OtherViewHolder mOtherViewHolder;
		if (convertView != null
				&& (convertView.getTag() instanceof OtherViewHolder)) {
			mOtherViewHolder = (OtherViewHolder) convertView.getTag();
		} else {
			mOtherViewHolder = new OtherViewHolder();
			convertView = View.inflate(context, R.layout.myregulardebt_item,
					null);
			mOtherViewHolder.refund_name = (TextView) convertView
					.findViewById(R.id.refund_name);
			mOtherViewHolder.interest_tv = (TextView) convertView
					.findViewById(R.id.interest_tv);
			mOtherViewHolder.deadline_company_tv = (TextView) convertView
					.findViewById(R.id.deadline_company_tv);
			mOtherViewHolder.bid = (TextView) convertView
					.findViewById(R.id.bid);
			mOtherViewHolder.title = (RelativeLayout) convertView
					.findViewById(R.id.title);
			mOtherViewHolder.centre = (LinearLayout) convertView
					.findViewById(R.id.centre);
			mOtherViewHolder.progressBar1 = (RoundProgressBar) convertView
					.findViewById(R.id.progressBar1);
			mOtherViewHolder.term = (TextView) convertView
					.findViewById(R.id.term);
			mOtherViewHolder.company_tv = (TextView) convertView
					.findViewById(R.id.company_tv);
			mOtherViewHolder.top = (RelativeLayout) convertView
					.findViewById(R.id.top);
			mOtherViewHolder.company_tv1 = (TextView) convertView
					.findViewById(R.id.company_tv1);
			mOtherViewHolder.flag_color = convertView
					.findViewById(R.id.flag_color);
			mOtherViewHolder.flag_cantrans = convertView
					.findViewById(R.id.flag_cantrans);
			mOtherViewHolder.flag_cantrans_right = convertView
					.findViewById(R.id.flag_cantrans_right);
			mOtherViewHolder.ll_xishu = convertView.findViewById(R.id.ll_xishu);
			mOtherViewHolder.tv_title_left = (TextView) convertView
					.findViewById(R.id.tv_title_left);
			mOtherViewHolder.tv_title_mid = (TextView) convertView
					.findViewById(R.id.tv_title_mid);
			mOtherViewHolder.tv_xishu = (TextView) convertView
					.findViewById(R.id.tv_xishu);
			convertView.setTag(mOtherViewHolder);
		}
		// 标的名称
		mOtherViewHolder.refund_name.setText(mList.get(position).getBidTitle());
		// 标的颜色
		String colorString = mList.get(position).getBidTypeColor();
		if (!TextUtils.isEmpty(colorString)) {
			mOtherViewHolder.flag_color.setBackgroundColor(Color
					.parseColor(colorString));
		}
		if (type == 1) {// 持有中
			mOtherViewHolder.tv_title_left.setText("投资金额(元)");
			mOtherViewHolder.tv_title_mid.setText("剩余期限");
			mOtherViewHolder.ll_xishu.setVisibility(View.GONE);
			// 是否可转让
			if (mList.get(position).getIsTrans() == 1) {
				mOtherViewHolder.flag_cantrans.setVisibility(View.VISIBLE);
				mOtherViewHolder.flag_cantrans_right.setVisibility(View.GONE);
				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						int width = mOtherViewHolder.flag_cantrans.getWidth();
						if (width < width_should) {
							mOtherViewHolder.flag_cantrans
									.setVisibility(View.GONE);
							mOtherViewHolder.flag_cantrans_right
									.setVisibility(View.VISIBLE);
						}
					}
				}, 60);

			} else {
				mOtherViewHolder.flag_cantrans.setVisibility(View.GONE);
				mOtherViewHolder.flag_cantrans_right.setVisibility(View.GONE);
			}
			// 投标金额
			mOtherViewHolder.interest_tv.setText(mList.get(position)
					.getPoiAmount());
			// 投标期限
			String remainTerm = mList.get(position).getRemainTerm();
			if (!TextUtils.isEmpty(remainTerm)) {
				if (remainTerm.contains("个")) {
					mOtherViewHolder.term.setText(remainTerm.substring(0,
							remainTerm.indexOf("个")));
					mOtherViewHolder.company_tv.setText(remainTerm
							.substring(remainTerm.indexOf("个")));
				} else if (remainTerm.contains("天")) {
					mOtherViewHolder.term.setText(remainTerm.substring(0,
							remainTerm.indexOf("天")));
					mOtherViewHolder.company_tv.setText(remainTerm
							.substring(remainTerm.indexOf("天")));
				} else {
					mOtherViewHolder.term.setText(remainTerm);
					mOtherViewHolder.company_tv.setText("");
				}
			} else {
				mOtherViewHolder.term.setText("");
				mOtherViewHolder.company_tv.setText("");
			}
			// 持有份数
			mOtherViewHolder.deadline_company_tv.setText("持有份数: "
					+ mList.get(position).getParts() + "份");
			// 进度条
			String bidAmount = mList.get(position).getBidAmount();
			String bidFreeAmount = mList.get(position).getBidFreeAmount();
			double interest = (Double.parseDouble(bidAmount) - Double
					.parseDouble(bidFreeAmount))
					/ Double.parseDouble(bidAmount);
			int interest1 = (int) (interest * 100);
			String interest1_string = df_point2.format(interest * 100);
			mOtherViewHolder.progressBar1.setTextSize(SystemUtils.dip2px(
					context, 13));
			mOtherViewHolder.progressBar1.setRoundWidth(SystemUtils.dip2px(
					context, 3));
			// 进度色值
			mOtherViewHolder.progressBar1.setCricleProgressColor(Color
					.parseColor("#ff841f"));
			// 圆环色值
			mOtherViewHolder.progressBar1.setCricleColor(Color
					.parseColor("#eeeeee"));
			// 进度字体色值
			mOtherViewHolder.progressBar1.setTextColor(Color
					.parseColor("#ff841f"));
			mOtherViewHolder.progressBar1.setPropress(interest1_string + "%");
			mOtherViewHolder.progressBar1.setProgress(interest1);
			// 标的状态
			String bidStatusCode = mList.get(position).getStatus();
			mOtherViewHolder.bid.setVisibility(View.GONE);
			mOtherViewHolder.bid
					.setBackgroundResource(R.drawable.tender_img_returning);
			mOtherViewHolder.progressBar1.setVisibility(View.GONE);
			if ("5002".equals(bidStatusCode)) {
				mOtherViewHolder.progressBar1.setVisibility(View.VISIBLE);
			} else if ("5003".equals(bidStatusCode)) {
				mOtherViewHolder.bid.setVisibility(View.VISIBLE);
				mOtherViewHolder.bid
						.setBackgroundResource(R.drawable.tender_img_man);
			} else if ("5004".equals(bidStatusCode)) {
				mOtherViewHolder.bid.setVisibility(View.VISIBLE);
				mOtherViewHolder.bid
						.setBackgroundResource(R.drawable.tender_img_returning);
			}
		} else if (type == 2) {// 转让中
			mOtherViewHolder.tv_title_left.setText("预期转让金额(元)");
			mOtherViewHolder.tv_title_mid.setText("剩余期限");
			mOtherViewHolder.ll_xishu.setVisibility(View.GONE);
			mOtherViewHolder.flag_cantrans.setVisibility(View.GONE);
			// 投标金额
			mOtherViewHolder.interest_tv.setText(df.format(mList.get(position)
					.getExpectAmountLong() * 0.01));
			// 投标期限
			String remainTerm = mList.get(position).getRemainTerm();
			if (!TextUtils.isEmpty(remainTerm)) {
				if (remainTerm.contains("个")) {
					mOtherViewHolder.term.setText(remainTerm.substring(0,
							remainTerm.indexOf("个")));
					mOtherViewHolder.company_tv.setText(remainTerm
							.substring(remainTerm.indexOf("个")));
				} else if (remainTerm.contains("天")) {
					mOtherViewHolder.term.setText(remainTerm.substring(0,
							remainTerm.indexOf("天")));
					mOtherViewHolder.company_tv.setText(remainTerm
							.substring(remainTerm.indexOf("天")));
				} else {
					mOtherViewHolder.term.setText(remainTerm);
					mOtherViewHolder.company_tv.setText("");
				}
			} else {
				mOtherViewHolder.term.setText("");
				mOtherViewHolder.company_tv.setText("");
			}
			// 转让系数
			mOtherViewHolder.deadline_company_tv
					.setText("转让系数: "
							+ df_point2.format(mList.get(position)
									.getTransFactor() * 0.01) + "%");
			// 进度条
			mOtherViewHolder.progressBar1.setTextSize(SystemUtils.dip2px(
					context, 13));
			mOtherViewHolder.progressBar1.setRoundWidth(SystemUtils.dip2px(
					context, 3));
			// 进度色值
			mOtherViewHolder.progressBar1.setCricleProgressColor(Color
					.parseColor("#1AB59A"));
			// 圆环色值
			mOtherViewHolder.progressBar1.setCricleColor(Color
					.parseColor("#eeeeee"));
			// 进度字体色值
			mOtherViewHolder.progressBar1.setTextColor(Color
					.parseColor("#1AB59A"));
			// 设置进度
			int progress_trans;
			String progress_trans_string;
			if (mList.get(position).getExpectParts() == 0) {
				progress_trans = 0;
				progress_trans_string = "0.00";
			} else {
				progress_trans = (int) (100f * mList.get(position)
						.getFinishParts() / mList.get(position)
						.getExpectParts());
				progress_trans_string = df_point2.format(100f
						* mList.get(position).getFinishParts()
						/ mList.get(position).getExpectParts());
			}
			mOtherViewHolder.progressBar1.setPropress(progress_trans_string
					+ "%");
			mOtherViewHolder.progressBar1.setProgress(progress_trans);
			// 标的状态
			mOtherViewHolder.bid.setVisibility(View.GONE);
			mOtherViewHolder.progressBar1.setVisibility(View.VISIBLE);
		} else if (type == 3) {// 已转让
			mOtherViewHolder.tv_title_left.setText("成交金额(元)");
			mOtherViewHolder.tv_title_mid.setText("成交份数");
			mOtherViewHolder.ll_xishu.setVisibility(View.VISIBLE);
			mOtherViewHolder.bid.setVisibility(View.GONE);
			mOtherViewHolder.progressBar1.setVisibility(View.GONE);
			mOtherViewHolder.flag_cantrans.setVisibility(View.GONE);
			// 投标金额
			mOtherViewHolder.interest_tv.setText(mList.get(position)
					.getAmount());
			// 成交份数
			mOtherViewHolder.term.setText(mList.get(position).getParts() + "");
			mOtherViewHolder.company_tv.setText("份");
			// 转让系数
			mOtherViewHolder.tv_xishu.setText(df_point2.format(mList.get(
					position).getTransFactor() * 0.01));
			// 成交完成时间
			mOtherViewHolder.deadline_company_tv.setText("最后成交: "
					+ mList.get(position).getFinishTime());
		} else if (type == 4) {// 已还记录
			mOtherViewHolder.tv_title_left.setText("投资金额(元)");
			mOtherViewHolder.tv_title_mid.setText("投资期限");
			mOtherViewHolder.ll_xishu.setVisibility(View.GONE);
			mOtherViewHolder.flag_cantrans.setVisibility(View.GONE);
			// 投标金额
			mOtherViewHolder.interest_tv.setText(mList.get(position)
					.getPoiAmount());
			// 投标期限
			String poiTerm = mList.get(position).getPoiTerm();
			if (!TextUtils.isEmpty(poiTerm)) {
				if (poiTerm.contains("个")) {
					mOtherViewHolder.term.setText(poiTerm.substring(0,
							poiTerm.indexOf("个")));
					mOtherViewHolder.company_tv.setText(poiTerm
							.substring(poiTerm.indexOf("个")));
				} else if (poiTerm.contains("天")) {
					mOtherViewHolder.term.setText(poiTerm.substring(0,
							poiTerm.indexOf("天")));
					mOtherViewHolder.company_tv.setText(poiTerm
							.substring(poiTerm.indexOf("天")));
				} else {
					mOtherViewHolder.term.setText(poiTerm);
					mOtherViewHolder.company_tv.setText("");
				}
			} else {
				mOtherViewHolder.term.setText("");
				mOtherViewHolder.company_tv.setText("");
			}
			// 持有份数
			mOtherViewHolder.deadline_company_tv.setText("持有份数: "
					+ mList.get(position).getParts() + "份");
			// 标的状态
			mOtherViewHolder.progressBar1.setVisibility(View.GONE);
			mOtherViewHolder.bid.setVisibility(View.VISIBLE);
			mOtherViewHolder.bid
					.setBackgroundResource(R.drawable.tender_img_returnd);
		}
		// 投标详情
		mOtherViewHolder.top.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				Intent intent = new Intent(context, MyDebtDetailActivity.class);
				intent.putExtra("poi_id", mList.get(position).getPoiId());
				intent.putExtra("bidId", mList.get(position).getBidId());
				intent.putExtra("transId", mList.get(position).getTransId());
				intent.putExtra("type", type);
				context.startActivity(intent);
			}
		});

		return convertView;
	}

	class OtherViewHolder {
		TextView refund_name;
		TextView interest_tv;
		TextView deadline_company_tv;
		TextView bid;
		TextView term;
		RelativeLayout title, top;
		LinearLayout centre;
		RoundProgressBar progressBar1;
		TextView company_tv, company_tv1;
		View flag_color, flag_cantrans, ll_xishu, flag_cantrans_right;
		TextView tv_title_left, tv_title_mid, tv_xishu;
	}

}
